
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'fun/emp/statisticsList',
        datatype: "json",
        colModel: [
            { label: '员工ID', name: 'empId', index: "emp_id", width: 30, key: true},
            { label: '姓名', name: 'empName',sortable: false, width: 100 },
            { label: '身份证号', name: 'idCard',sortable: false, width: 100 },
            { label: '所属部门', name: 'deptName', sortable: false, width: 100 },
            { label: '职名（务）', name: 'office', sortable: false, width: 100 },
            { label: '状态', name: 'status', sortable: false,width: 50, formatter: function(value, options, row){
                    return value === 0 ?
                        '<span >在岗</span>' :
                        '<span >不在岗</span>';
                }},
            { label: '备注', name: 'remarks', index: 'remarks', width: 100 }
        ],
        viewrecords: true,
        height: 400,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            $("#jqGrid").setGridParam().hideCol("empId").trigger("reloadGrid");
        },
    });
});
//部门结构树
var dept_ztree;
var dept_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            status:'',
            changeDate:null,
            queryByDate:[],
            gender:null,
            office:null,
            deptId:null,
            deptName:null,
            empName:null,
            education:[],
            background:[],
        },
        showList: true,
        title: null,
        pickerOptions: {
            disabledDate(time) {
                return time.getTime() > Date.now();
            }
        },
        politicalBackground: [{value: '群众'},{value:'团员'},{value:'预备党员'},{value:'党员'}],
        education: [{value:'大专'},{value:'本科'},{value:'硕士'},{value:'博士'}],
        statusType: [{value:'0',label:'在岗'},{value:'5',label:'不在岗'}],
        causes: [{value:'7',label:'产假结束'},{value:'8',label:'停薪留职结束'},{value:'9',label:'长病假结束'}],
        fileList: [],
        dialogFormVisible: false,
        dialog: false,
        formLabelWidth: '80px',
        timer: null,
        timeout: null
    },

    methods: {
        filterSelect:function ()
        {
            vm.dialogFormVisible=true;
            vm.getDict();
            vm.getDept();
        },
        resetForm(q) {
            vm.q={
                status:'',
                changeDate:null,
                queryByDate:[],
                gender:null,
                office:null,
                deptId:null,
                deptName:null,
                empName:null,
                education:[],
                background:[],
            }
        },
        querySearchAsync: function (queryString, cb) {
            var dict = vm.dict;
            var results = queryString ? dict.filter(this.createStateFilter(queryString)) : dict;

            clearTimeout(this.timeout);
            this.timeout = setTimeout(() => {
                cb(results);
            }, 3000 * Math.random());
        },
        createStateFilter: function (queryString) {
            return (office) => {
                return (office.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
            };
        },
        handleSelect: function (item) {
            console.log(item);
        },
        getDict: function () {
            $.get(baseURL + "fun/emp/dictList", function (r) {
                vm.dict = r.dict;
            });
        },
        query: function () {
            vm.reload();
        },
        empShow:function ()
        {
            var empId = getSelectedRow();
            if(empId == null){
                return ;
            }
            window.location.href="statistics_emp_show.html?empId="+empId;
        },
        exportExcel: function (event) {
            var empIds = getSelectedRows();
            if(empIds == null){
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "fun/emp/exportEmps",
                contentType: "application/json",
                data: JSON.stringify(empIds),
            });
            vm.$message('导出到G://empExcel成功');
        },

        handlePreview:function(file) {
            console.log(file);
        },
        handleRemove:function(file, fileList) {
            console.log(file, fileList);
        },
        fresh:function (event)
        {
            $("#jqGrid").trigger("reloadGrid");
            vm.dialogFormVisible=false;
        },
        freshDept:function (event)
        {
            $("#jqGrid").trigger("reloadGrid");
            vm.dialogDeptVisible=false;
        },
        filterOption:function (event)
        {
            /*关闭dialog*/
            vm.dialogFormVisible=false;
            vm.showList = false;
            vm.reload();
        },
        getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/list", function(r){
                dept_ztree = $.fn.zTree.init($("#deptTree"), dept_setting , r);
                var node = dept_ztree.getNodeByParam("deptId", vm.q.deptId);
                if(node != null){
                    dept_ztree.selectNode(node);
                    vm.q.deptName = node.name;
                }
            })
        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = dept_ztree.getSelectedNodes();
                    //选择上级部门
                    vm.q.deptId = node[0].deptId;
                    vm.q.deptName = node[0].name;
                    layer.close(index);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            if(vm.queryByDate==null)
            {
                vm.queryByDate=['',''];
            }
            var jsonEducation = JSON.stringify(vm.q.education)
            var jsonBackground = JSON.stringify(vm.q.background)
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'dateStart': vm.queryByDate[0],'dateEnd': vm.queryByDate[1],
                    'deptId':vm.q.deptId,'gender':vm.q.gender,
                    'office':vm.q.office,'empName':vm.q.empName,
                    'education':jsonEducation,'background':jsonBackground ,
                    'status':vm.q.status
                },
                page:page
            }).trigger("reloadGrid");
        }
    }
});
window.addEventListener("message",function(event){
    vm.showList = true;
    if(vm.queryByDate==null)
    {
        vm.queryByDate=['',''];
    }
    var page = $("#jqGrid").jqGrid('getGridParam','page');
    $("#jqGrid").jqGrid('setGridParam',{
        postData:{'deptId':event.data,'dateStart': vm.queryByDate[0],'dateEnd': vm.queryByDate[1]},
        page:page
    }).trigger("reloadGrid");
}, false);